{ Statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=1375
  Verdict: Accepted
}
uses
	SysUtils, Math; 
 
var
	n,m,i,x,y,ans:longint;
	p,r:array[0..20010] of longint;
 
procedure makeSet(v:longint);
begin
p[v] := v;
r[v] := 0;
end;
 
function findSet(v:longint):longint;
begin
if v = p[v] then begin
	result := v;
	exit;
end;
p[v] := findSet(p[v]);
result := p[v];
end;
 
procedure unionSets(a,b:longint);
 
var
	t:longint;
 
begin
a := findSet(a);
b := findSet(b);
if a <> b then begin
	if r[a] < r[b] then begin
		t := a;
		a := b;
		b := t;
	end;
	p[b] := a;
	if r[a] = r[b] then inc(r[a]);
end;
end;
 
begin
reset(input,'input.txt');
rewrite(output,'output.txt');
read(n, m);
ans := 0;
for i := 1 to n do 
	makeSet(i);
for i := 1 to m do begin
	read(x, y);
	if findSet(x) <> findSet(y) then ans := i;
	unionSets(x, y);	
end;
write(ans);
end.